package com.iot08.api.service;

import com.iot08.common.entity.app.ComplaintReason;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Description: 投诉原因
 * @author: Rock
 * @time: 2021/5/7 11:28
 */
@Slf4j
@Service
public class ComplaintReasonService extends BaseService<ComplaintReason> {

    @Value("${complaint.domain}")
    private String domain;

    /**
     * @time: 2021/5/7 11:32
     * @author: Rock
     * @version 1.0
     */
    public ResponseMap listData() {
        List<Object> list = new ArrayList<>();
        Query query = new Query();
        //投诉的原因列表 从数据库中读取的, 目前增加是手动录入数据库的
        List<ComplaintReason> records = this.find(query.with(Sort.by(Sort.Order.desc("_id"))));
        for (ComplaintReason record : records) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", record.getId());
            temp.put("content", record.getContent());
            list.add(temp);
        }
        ResponseMap res = ResponseMap.success();
        res.put("list", list);
        //res.put("complaintExplainUrl", "http://" + domain + "/getway/complaint_procedure");
        res.put("complaintExplainUrl", "https://" + domain + "/agreement/complaint_procedure.html");
        return res;
    }

    /**
     * @description 添加投诉内容
     * @author cwj
     * @param [content]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2024/12/17 18:11
     **/
    public ResponseMap addData(String content){
        ComplaintReason cr = new ComplaintReason();
        cr.setContent(content);
        cr.setCreateTime(System.currentTimeMillis()/1000);
        this.save(cr);
        return ResponseMap.success();
    }

}
